<template>
  <PageMain :title="id ? '编辑' :'新增'">
    <div class="form-content f-c j-b">
      <div class="form-box">
        <el-form ref="formRef" :model="formData" :rules="rules" label-position="top" >
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="分类名称" prop="name">
                <el-input clearable v-model="formData.name"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="上级分类" prop="parentId">
                <el-select v-model="formData.parentId">
                  <el-option v-for="item in categoryList" :label="item.name" :value="item.id" :key="item.id"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="描述" prop="description">
                <el-input v-model="formData.description" clearable type="textarea" :rows="5"></el-input>
              </el-form-item>
            </el-col>

          </el-row>
        </el-form>
      </div>
      <div class="form-button f-s">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="confirm">确认</el-button>
      </div>
    </div>
  </PageMain>
</template>
<script setup>
import {ref} from "vue";
import { useRouter, useRoute } from "vue-router";
import {ElLoading, ElMessage} from "element-plus";
import { http } from "@/api";
const apiKey = 'productCategory'
const router = useRouter()
const route = useRoute()
const id = route.params.id
const formData = ref({
  name: '',
  description: '',
  parentId: ''
})
const rules = ref({
  name: [
    { required: true, message: '请输入', trigger: 'blur' },
  ],
})
const categoryList = ref([])
http.productCategory.list().then(res => {
  categoryList.value = res.data
})
const formRef =  ref(null)
if (id) {
  // 获取详情
  http[apiKey].read({
    id
  }).then(res => {
    console.log(res)
    for (const valueKey in formData.value) {
      formData.value[valueKey] = res.data[valueKey]
    }
    formData.value.id = id
  })
}
const cancel = () => {
  router.back()
}
const confirm = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      const loadingInstance = ElLoading.service()
      if (id) {
        // 编辑
        http[apiKey].update({
          ...formData.value,
          id
        }).then(res => {
          console.log(res)
          if (res.code === 0) {
            ElMessage.success('编辑成功')
            router.back()
          }
        }).finally(() => {
          loadingInstance.close()
        })
      } else {
        // 新增
        http[apiKey].create(formData.value).then(res => {
          console.log(res)
          if (res.code === 0) {
            ElMessage.success('新增成功')
            router.back()
          }
        }).finally(() => {
          loadingInstance.close()
        })
      }
    } else {
      console.log('error submit!!')
      return false
    }
  })
}
</script>

<style scoped lang="scss">
.form-box {
  display: flex;
  .gap {
    width: 32px;
  }
}
</style>
